from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from .models import User


# 判断用户是否存在  /users/usernames/(?P<username>\w{5, 20})/count
class RegisterUserAPIView(APIView):

    def get(self, request, username):
        count = User.objects.filter(username=username).count()
        context = {
            'count': count,
            'username': username
        }
        return Response(context)


# 判断手机号是否存在  /users/phones/(?P<mobile>1[345789]\d{9})/count/
class RegisterMobileAPIView(APIView):

    def get(self, request, mobile):
        count = User.objects.filter(mobile=mobile).count()
        context = {
            'count': count,
            'mobile': mobile
        }
        return Response(context)
